﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace IMCOM.Imagiality.ImageProcessing.Canvas2D.AutoContrast
{
    public class AutoContrastLineDrawn : Canvas2DAutoContrastState
    {
        private AutoContrastHandle startPointHandle;
        private AutoContrastHandle endPointHandle;
        private bool mouseOverStartPointHandle;
        private bool mouseOverEndPointHandle;

     
        public AutoContrastLineDrawn(AutoContrastImageEventListener controller)
            : base(controller)
        {
            createHandles();
        }
        
        protected void createHandles()
        {
            startPointHandle = new AutoContrastHandle(controller.Calculator.LineStartPoint);
            endPointHandle = new AutoContrastHandle(controller.Calculator.LineEndPoint);
        }

        public override void MouseDown(object sender, MouseEventArgs e)
        {
            Canvas2DAutoContrastState newState;

            if (mouseOverStartPointHandle == true)
            {
                newState = new AutoContrastLineStartPointScaling(controller, startPointHandle);
            }
            else if (mouseOverEndPointHandle == true)
            {
                newState = new AutoContrastLineEndPointScaling(controller, endPointHandle);
            }
            else
            {
                newState = new FirstPointSelection(controller);
            }

            SetControllerState(newState);
        }

        public override void MouseUp(object sender, MouseEventArgs e)
        {
        }

        public override void MouseMoved(object sender, MouseEventArgs e)
        {
            if (startPointHandle.IsWithinHandle(new Point(e.X, e.Y)))
            {
                mouseOverStartPointHandle = true;
                controller.SetCursor(startPointHandle.GetCursor());
            }
            else
            {
                mouseOverStartPointHandle = false;

                if (endPointHandle.IsWithinHandle(new Point(e.X, e.Y)))
                {
                    mouseOverEndPointHandle = true;
                    controller.SetCursor(endPointHandle.GetCursor());
                }
                else
                {
                    mouseOverEndPointHandle = false;
                    controller.SetCursor(Cursors.Default);
                }
            }

            controller.RefreshCanvas();
        }

        public override void ImagePainted(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            Pen pen = new Pen(Color.Green, 3);
            pen.EndCap = System.Drawing.Drawing2D.LineCap.Custom;
            pen.CustomEndCap = new System.Drawing.Drawing2D.AdjustableArrowCap(5, 5);

            g.DrawLine(pen, controller.Calculator.LineStartPoint, controller.Calculator.LineEndPoint);

            startPointHandle.Display(g);
            endPointHandle.Display(g);
        }
    }
}
